package com.controller.sys.sysOperLog;

import com.model.base.PageResult;
import com.model.base.Result;
import com.common.anntation.Log;
import com.common.emums.BusinessType;
import com.common.emums.DataStatus;
import com.common.excel.ExcelUtil;
import com.common.exception.BusinessException;
import com.common.utils.DateUtils;
import com.common.utils.EntityConversionUtil;
import com.model.dto.sys.sysOperLog.GetSysOperLogListDTO;
import com.model.dto.sys.sysOperLog.SysOperLogExcel;
import com.model.dto.sys.sysOperLog.UpdateSysOperLogStatusDTO;
import com.github.pagehelper.PageInfo;
import com.model.pojo.sys.SysOperLog;
import com.service.sys.SysOperLogService;
import com.model.vo.sys.sysOperLog.GetSysOperLogVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @Author: zql
 * @Description: 操作日志模块
 * @Data: 2021-08-06
 */
@Api(tags = {"操作日志"})
@Slf4j
@RestController
@RequestMapping("/v1")
public class SysOperLogController {
    @Resource
    SysOperLogService sysOperLogService;

    /**
    * @Author: zql
    * @Description: 查看操作日志列表
     * @param getSysOperLogListDTO 请求数据
     * @Data: 2021-08-06
     */
    @ApiOperation(value = "查看操作日志列表")
    @PostMapping("/getSysOperLogList")
    public Result<PageResult<List<GetSysOperLogVO>>> getSysOperLogList(@RequestBody @Valid GetSysOperLogListDTO getSysOperLogListDTO) {
        List<SysOperLog> sysOperLogList = sysOperLogService.getList(getSysOperLogListDTO.getTitle(),getSysOperLogListDTO.getBusinessType(),getSysOperLogListDTO.getStatus(),getSysOperLogListDTO);
        PageInfo<SysOperLog> pageInfo = new PageInfo<>(sysOperLogList);
        List<GetSysOperLogVO> getSysOperLogVOList = EntityConversionUtil.copyList(sysOperLogList, GetSysOperLogVO.class);
        PageResult<List<GetSysOperLogVO>> build = PageResult.<List<GetSysOperLogVO>>builder().result(getSysOperLogVOList).pageInfo(pageInfo).build();
        return Result.suc(build);
    }

    /**
     * @Author: zql
     * @Description: 修改操作日志状态
     * @param updateSysOperLogStatusDTO 请求数据
     * @Data: 2021-08-06
     */
    @ApiOperation(value = "修改操作日志状态")
    @Log(title = "修改操作日志状态", businessType = BusinessType.UPDATE)
    @PostMapping("/updateSysOperLogStatus")
    public Result<Objects> updateSysOperLogStatus(@RequestBody @Valid UpdateSysOperLogStatusDTO updateSysOperLogStatusDTO) {
        SysOperLog sysOperLogDB = sysOperLogService.get(updateSysOperLogStatusDTO.getSysOperLogId(),null);
        if(DataStatus.OK.equals(updateSysOperLogStatusDTO.getIsDelete()) && !DataStatus.OK.equals(sysOperLogDB.getIsDelete())){
            log.debug("数据从停用/删除状态改成正常状态,需要做唯一性校验从这执行");
        }
        SysOperLog sysOperLog = new SysOperLog();
        sysOperLog.setSysOperLogId(updateSysOperLogStatusDTO.getSysOperLogId());
        sysOperLog.setIsDelete(updateSysOperLogStatusDTO.getIsDelete());
        sysOperLogService.update(sysOperLog);
        return Result.suc();
    }

   /**
     * @Author: zql
     * @Description: 导出操作日志表
     * @param getSysOperLogListDTO 请求数据
     * @param response 返回内容
     * @Data: 2021-08-06
     */
    @Log(title = "导出操作日志表", businessType = BusinessType.EXPORT,  isSaveResponseData = false)
    @ApiOperation(value = "导出操作日志表")
    @PostMapping(value = "/exportSysOperLog")
    public void exportSysOperLog(@RequestBody @Valid GetSysOperLogListDTO getSysOperLogListDTO, HttpServletResponse response) throws BusinessException {
        List<SysOperLog> sysOperLogList = sysOperLogService.getList(getSysOperLogListDTO.getTitle(),getSysOperLogListDTO.getBusinessType(),getSysOperLogListDTO.getStatus(),getSysOperLogListDTO);
        List<SysOperLogExcel> sysOperLogExcelList = new ArrayList<>();
        for (SysOperLog sysOperLog : sysOperLogList) {
            SysOperLogExcel sysOperLogExcel = new SysOperLogExcel();
            BeanUtils.copyProperties(sysOperLog, sysOperLogExcel);
            sysOperLogExcel.setBusinessType(sysOperLog.getBusinessType().intValue());
            sysOperLogExcel.setUserType(sysOperLog.getUserType().intValue());
            sysOperLogExcel.setStatus(sysOperLog.getStatus().intValue());
            sysOperLogExcelList.add(sysOperLogExcel);
        }
        String fileName = "操作日志信息表" + DateUtils.parseDateToStr(DateUtils.YYYYMMDD_HHMM, new Date());
        try {
            ExcelUtil.writeExcel(response, sysOperLogExcelList, fileName, "操作日志信息", new SysOperLogExcel());
        } catch (IOException e) {
            log.error("导出失败，文件名:{}", fileName);
        }
    }
}